$(function () {
    //0 复选框全选事件
    $("#convenientInformationShowFather").click(function () {
        var isChecked = $(this).prop("checked");
        if (isChecked) {
            $(".conveninentInformationShowSon").prop("checked", true);
        } else {
            $(".conveninentInformationShowSon").prop("checked", false);
        }
    });

    //1 获取所有数据
    $.ajax({
        url: "/hospital/convenient/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $list = $("#tb_convenient");
            $.each(data, function (index, ele) {
                var $item = createDataItem(index, ele);
                $list.append($item);
            });
        },
        error: function () {
            alert("获取数据失败");
        }
    });

    //动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + "<tr>\n" +
            "            <td style=\"width:30px;\"><input class=\"conveninentInformationShowSon\" type=\"checkbox\"></td>\n" +
            "            <td>" + data.patName + "</td>\n" +
            "            <td>" + data.patSex + "</td>\n" +
            "            <td>" + data.patAge + "</td>\n" +
            "            <td>" + data.patContactInfo + "</td>\n" +
            "            <td>" + data.patDiseaseHistory + "</td>\n" +
            "            <td>" + data.patConven + "</td>\n" +
            "            <td>\n" +
            "                <button data-id=\"" + data.id + "\" class=\"convenientInformationShowUpdateBtn\">编辑</button>\n" +
            "                <button data-id=\"" + data.id + "\" class=\"convenientInformationShowCancelBtn\">删除</button>\n" +
            "            </td>\n" +
            "        </tr>");
        return $item;
    }

    //动态刷新表格
    function createNewData(data) {
        //发送数据成功，返回成功数据，隐藏新增信息界面，显示表格数据
        var $list = $("#tb_convenient");
        //1.清空表格数据
        $("#tb_convenient tr:not(:first)").html("");
        //2.动态创建数据
        $.each(data, function (index, ele) {
            var $item1 = createDataItem(index, ele);
            $list.append($item1);
        });
    }

    //2 新增按钮监听事件
    $("#convenientOperAddBtn").click(function () {
        $(".conveninetInformationShow").css("display", "none");
        $(".convenientAdd").css("display", "block");
    });
    //2.1 确认按钮监听事件
    $("#convenientAddSureBtn").click(function () {
        //2.1.1 获取输入的数据
        var name = $("#convenientAddInputName").val();
        var sex = $("#convenientAddInputSex").val();
        var age = $("#convenientAddInputAge").val();
        var nation = $("#convenientAddInputNation").val();
        var contact = $("#convenientAddInputContact").val();
        var disease = $("#convenientAddInputDisHis").val();
        //2.2.2发送数据
        $.ajax({
            url: "/hospital/convenient/add.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "patName": name, "patSex": sex, "patAge": age, "patNation": nation,
                "patContactInfo": contact, "patDiseaseHistory": disease
            }),
            success: function (data) {
                createNewData(data);  //刷新表格内容
                alert("添加成功");
                $(".convenientAdd").css("display", "none");
                $(".conveninetInformationShow").css("display", "block");
                // $(".convenientNumShow").css("display", "block");
            },
            error: function () {
                alert("添加失败");
            }
        });
    });
    //2.2 返回按钮监听事件
    $("#convenientAddBackBtn").click(function () {
        $(".convenientAdd").css("display", "none");
        $(".conveninetInformationShow").css("display", "block");
        // $(".convenientNumShow").css("display", "none");
    });

    //2.3 输入框失去焦点判断
    function judgeInput(value, judgeId, judgeName) {
        if (value.trim().length == 0) {
            $("#" + judgeId).html("/*" + judgeName + "不能为空*/");
        } else {
            $("#" + judgeId).remove();
        }
    }

    $("#convenientAddInputName").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeName", "病人名称");
    });
    $("#convenientAddInputSex").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeSex", "病人性别");
    });
    $("#convenientAddInputAge").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeAge", "病人年龄");
    });
    $("#convenientAddInputNation").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeNation", "病人民族");
    });
    $("#convenientAddInputContact").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeContact", "联系方式");
    });
    $("#convenientAddInputDisHis").on("blur", function () {
        var value = $(this).val();
        judgeInput(value, "judgeDisease", "过往病史");
    });

    //3 操作栏编辑 删除按钮事件
    //3.1 编辑按钮事件委托监听
    $("#tb_convenient").delegate(".convenientInformationShowUpdateBtn", "click", function () {
        //3.1.1 获取修改前的数据
        var id = $(this).attr("data-id");
        var value = $(this).parents("tr")[0];
        var value1 = value.children[1].innerHTML;
        var value2 = value.children[2].innerHTML;
        var value3 = value.children[3].innerHTML;
        var value4 = value.children[4].innerHTML;
        var value5 = value.children[5].innerHTML;
        var value6 = value.children[6].innerHTML;
        $("#PatientName").val(value1);
        $("#patientSex").val(value2);
        $("#patientAge").val(value3);
        $("#patientContact").val(value4);
        $("#patientDisHis").val(value5);
        $("#patientConven").val(value6);
        //显示编辑界面
        $(".convenientClickUpdateGetInfo").css("display", "block");
        $("#over").css("display", "block");

        //3.1.2 编辑界面 确认按钮点击事件
        $("#patientclickUpdateGetInfoSureBtn").unbind("click").click(function () {
            //获取用户修改后的值
            var name = $("#PatientName").val();
            var sex = $("#patientSex").val();
            var age = $("#patientAge").val();
            var contact = $("#patientContact").val();
            // alert("联系方式"+contact);
            var disease = $("#patientDisHis").val();
            // alert("病史"+disease);
            $.ajax({
                url: "/hospital/convenient/update.action",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                data: JSON.stringify({
                    "id": id, "patName": name, "patSex": sex, "patAge": age,
                    "patContactInfo": contact, "patDiseaseHistory": disease
                }),
                success: function (data) {
                    createNewData(data);
                    alert("修改成功");
                    $(".convenientClickUpdateGetInfo").css("display", "none");
                    $("#over").css("display", "none");
                },
                error: function () {
                    alert("修改失败");
                }
            });
        });
        //3.1.3 编辑页面 返回按钮监听
        $("#patientclickUpdateGetInfoBackBtn").click(function () {
            $(".convenientClickUpdateGetInfo").css("display", "none");
            $("#over").css("display", "none");
        });
    });
    //3.2 删除按钮事件委托监听
    $("#tb_convenient").delegate(".convenientInformationShowCancelBtn", "click", function () {
        var id = $(this).attr("data-id");
        $.ajax({
            url: "/hospital/convenient/delete.action",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data: JSON.stringify({"id": id}),
            success: function (data) {
                createNewData(data);
                alert("删除成功");
            },
            error: function () {
                alert("删除失败");
            }
        });
    });
});